<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/10/29 0029
 * Time: 17:02
 */

namespace Home\Controller;


use Think\Controller;

class IndexController extends Controller {

    public function index(){
        $this->redirect("sign");

    }
    public function sign(){
            if(IS_POST){
                if(M("User")->where(array("phone"=>I("post.phone")))->find()){
                    $this->error("该用户已经签到过");
                }else{
                    $config = array(
                        'maxSize' => 3145728,
                        'rootPath' => './Uploads/',
                        'savePath' => '',
                        'saveName' => array('uniqid',''),
                        'exts' => array('jpg', 'gif', 'png', 'jpeg'),
                        'autoSub' => true,
                        'subName' => array('date','Ymd'),
                    );
                    $upload = new \Think\Upload($config);// 实例化上传类
                    $info = $upload->upload();
                    if($info){
                        $data['pic'] = $info['pic']["savepath"].$info['pic']["savename"];
                        $data['phone'] = I("post.phone");
                        $data['name'] = I("username");
                        $data['uniqId'] = uniqid();
                        if(M("User")->add($data)){
                            $this->success('签到成功');
                        }
                    }
                }
            }else{
                $this->display('sign');
            }
    }

    public function lottery(){

            if(!session("user_id")){
                $this->assign("lottery",1);
                $this->display("Admin@User:index");

            }else{
                $userInfoList = M('user')->union("select * from cj_user")->union("select * from cj_virtual_user")->select();
                foreach($userInfoList as &$userInfo){
                    $userInfo["phone"] = substr_replace($userInfo["phone"],"****",3,6);
                }
                $idIndex= session('idIndex')?session('idIndex'):0;
                $lotIdList = M("Lottery")->getField("id",true);
                session("lotIdList",$lotIdList);
                session("lotteryId",$lotIdList[$idIndex]);
                //$this->assign('lotteryId',$lIdList[$idIndex]);
                $this->assign('userInfoList',$userInfoList);
                $this->assign('lotteryNum',I("post.lotteryNum"));
                $this->display('lottery');
            }



    }

    public function getLotteryUser( ){
//        $lotteryList = M("lottery")->select();
//        $userList = M("user")->field('uniqid')->union("select uniqid from cj_virtual_user")->select();
//        $lotteryUserUniqidList = array();
//        $total = 0;//所有奖项中奖名额只和
//        $setted = 0;//已设置中奖用户数
//        if($lotteryList){
//            if(I("post.abandon")){
//                $storeLottery = session('lotteryUser');
//                foreach($userList as $user){
//                    $userUniqidList[] = $user['uniqid'];
//                }
//                $storeLotteryUserList = array();
//                foreach( $storeLottery as $storeUser){
//                    $storeLotteryUserList = array_merge($storeLotteryUserList,$storeUser);
//                }
//                $unLotteryUserUniqidList = array_diff($userUniqidList,$storeLotteryUserList);//未中奖的用户
//                shuffle($unLotteryUserUniqidList);
//                $abandonLotteryData = I("post.abandon");
//                foreach($lotteryList as &$lottery){
//                    for($i = 0;$i<count($abandonLotteryData[$lottery["id"]]);$i++){
//                        $tempLottery[] = array_pop($unLotteryUserUniqidList);
//                    }
//
//                    $lottery['lotteryuser'] = $tempLottery;
//                    unset($tempLottery);
//                }
//
//            }else{
//                foreach($lotteryList as $lottery){
//                    $total += $lottery['num'];
//                    if($lottery['lotteryuser']){
//                        $templotteryUserUniqidList = explode(",",$lottery['lotteryuser']);
//                        $setted +=  count($templotteryUserUniqidList);
//                    }
//                }
//                if($total > $setted){
//                    foreach($lotteryList as $lottery){
//                        if($lottery['lotteryuser']){
//                            $templotteryUserUniqidList = explode(",",$lottery['lotteryuser']);
//                            $lotteryUserUniqidList = array_merge($lotteryUserUniqidList,$templotteryUserUniqidList);//已设定中奖的用户
//                        }
//                    }
//                    foreach($userList as $user){
//                        $userUniqidList[] = $user['uniqid'];
//                    }
//                    $unLotteryUserUniqidList = array_diff($userUniqidList,$lotteryUserUniqidList);//未中奖的用户
//                    shuffle($unLotteryUserUniqidList);
//                    //$toLotteryUserUniqidIndexList = array_rand($unLotteryUserUniqidList,$total-$setted);//随机抽出来的中奖用户
//                    //为每个奖项分配中奖用户
//                    foreach($lotteryList as &$lottery){
//                        $numSet = 0;
//                        if($lottery['lotteryuser']){
//                            $tempResult = explode(",",$lottery['lotteryuser']);
//                            $numSet = count($tempResult);
//                        }
//                        for($i=0;$i<$lottery["num"]-$numSet;$i++){
//                            $tempResult[] = array_pop($unLotteryUserUniqidList);
//                        }
//                        $lottery["lotteryuser"] = $tempResult;
//                    }
//
//                }else{
//                    //中奖名额全部被设置了
//                    foreach($lotteryList as &$lottery){
//                        if($lottery['lotteryuser']){
//                            $tempResult = explode(",",$lottery['lotteryuser']);
//                            $lottery["lotteryuser"] = $tempResult;
//                        }
//                    }
//
//                }
//
//            }
//
//            unset($lottery);
//            foreach($lotteryList as $lottery){
//                $tempLottery[$lottery["id"]] = $lottery["lotteryuser"];
//            }
//            if(!session("lotteryUser")){
//                session("lotteryUser", $tempLottery);
//            }else{
//                $tempSession = session("lotteryUser");
//                foreach( $tempSession as $key => &$val){
//                    $val = array_merge($val,$tempLottery[$key]);
//                }
//                session("lotteryUser",$tempSession);
//            }
//
//            }
//
//
//        $this->ajaxReturn($lotteryList);
          $tempUserList = M("user")->union("select * from cj_virtual_user")->select();
          foreach($tempUserList as $user){
              $userList[] = $user['uniqid'];
              $userNameList[$user['uniqid']] = $user["name"];
          }
          $lotteryId = session("lotteryId");
          $lottery = M("lottery")->where(array("id"=>$lotteryId)) ->find();
          $lotteryUserList = $_SESSION["lotteryUserList"][$lotteryId];
          $abandonList = $_SESSION["lotteryUserList"]["abandon"][$lotteryId];
          if(count($lotteryUserList) < $lottery["num"]){
              $setedLotteryUserList = explode(",",$lottery["lotteryuser"]);
              if(count($lotteryUserList) < count($setedLotteryUserList) && count($abandonList) < count($setedLotteryUserList)){
                 $unLotteryUserList = $lotteryUserList?array_diff($setedLotteryUserList,$lotteryUserList):$setedLotteryUserList;
                  $unLotteryUserList = $abandonList?array_diff($unLotteryUserList,$abandonList):$unLotteryUserList;
              }else{
                 $unLotteryUserList = $_SESSION["lotteryUserList"]["total"]?array_diff($userList,$_SESSION["lotteryUserList"]["total"]):$userList;
                  $unLotteryUserList =$_SESSION["lotteryUserList"]['abandon']['total']?array_diff($unLotteryUserList,$_SESSION["lotteryUserList"]['abandon']['total']):$unLotteryUserList;
              }
          }else{
               $idIndex = session('idIndex');
               if(++$idIndex  == count($_SESSION['lotIdList'])){
                   $data["1"] = -1;
                   $data["total"] = session("lotteryUserList");
               }else{
                   session("lotteryId",$_SESSION['lotIdList'][$idIndex]);
                   session("idIndex",$idIndex);
                   $data["result"] = 0;
               }

               $this->ajaxReturn($data);
               exit;
          }

          $result = array_rand($unLotteryUserList,1);
          $_SESSION["lotteryUserList"][$lotteryId][] = $unLotteryUserList[$result];
          $_SESSION["lotteryUserList"]["total"][] = $unLotteryUserList[$result];
          $data['result'] = 1;
          $data['name'] = $userNameList[$unLotteryUserList[$result]];
          $data['lotteryUniqid'] = $unLotteryUserList[$result];
          $this->ajaxReturn($data);
    }

    public function reGetLotteryUser(){

    }

    public function abandon(){
        $abandonUniqId = I("post.uniqId");
        $lotteryId = $_SESSION["lotteryId"];
        $_SESSION["lotteryUserList"]['abandon'][$lotteryId][] = $abandonUniqId;
        $_SESSION["lotteryUserList"]['abandon']['total'][] = $abandonUniqId;

        $lotteryUsertList = $_SESSION["lotteryUserList"][$lotteryId];
        $total = $_SESSION["lotteryUserList"]["total"];
        array_pop( $lotteryUsertList);
        $_SESSION["lotteryUserList"][$lotteryId] = $lotteryUsertList;
        array_pop( $total);
        $_SESSION["lotteryUserList"]['total'] = $total;
        $this->success("ok");


    }

}